package com.itSession.cn.controller.app;

import com.itSession.cn.entity.CircleInfo;
import com.itSession.cn.entity.CommentInfo;
import com.itSession.cn.mapper.CircleInfoMapper;
import com.itSession.cn.mapper.CommentInfoMapper;
import com.itSession.cn.util.Result;
import com.itSession.cn.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName: CommentInfoController
 * @description: CommentInfoController
 * @author: hanP
 * @create: 2021-06-18 14:09:56
 */
@Api(tags = "评论模块")
@RequestMapping("/commentInfo")
@RestController
@MapperScan("com.itSession.cn.mapper")
public class CommentInfoController {

    @Autowired
    private CommentInfoMapper commentInfoMapper;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("list")
    public Result listAll() {
    	try {
    		List<CommentInfo> list = commentInfoMapper.listAll();
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }


    @ApiOperation(value = "查询我的评价", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentAccount", value = "主键", required = false, paramType = "query" ,dataType="int")
    })
    @RequestMapping("listByAccount")
    public Result listByAccount(Integer commentAccount) {
        try {
            List<CommentInfo> list = commentInfoMapper.listByAccount(commentAccount);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询评价", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentCircleId", value = "主键", required = false, paramType = "query" ,dataType="int")
    })
    @RequestMapping("listByCircleId")
    public Result listByCircleId(String commentCircleId) {
        try {
            List<CommentInfo> list = commentInfoMapper.listByCircleId(commentCircleId);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByCircleIdAndCommentState")
    public Result listByCircleIdAndCommentState(String commentCircleId,String commentState) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("commentCircleId",commentCircleId);
            map.put("commentState",commentState);
            List<CommentInfo> list = commentInfoMapper.listByCircleIdAndCommentState(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }


    
     /**
     * 分页查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByParam")
    public Result listByParam(String keyword) {
    	try {
         	Map<String, Object> map = new HashMap<>();
            map.put("keyword",keyword);
    		List<CommentInfo> list = commentInfoMapper.listByParam(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }



    /**
     * 根据主键查询
     *
     * @param commentId 主键
     * @return 返回记录，没有返回null
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "POST", notes = "加载数据", response = CommentInfo.class)
     @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "主键", required = false, paramType = "query" ,dataType="int")
    })
    @RequestMapping("getById")
    public Result getById(@ApiIgnore Integer commentId) {
    	try {
    	 	CommentInfo commentInfo = commentInfoMapper.getById(commentId);
         	return ResultGenerator.genSuccessResult(commentInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500, e.getMessage());
        }
       
    }
    
    
    /**
     * 新增，忽略null字段
     *
     * @param commentInfo 新增的记录
     * @return 返回影响行数
     */
      @ApiOperation(value = "新增，忽略null字段", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
                @ApiImplicitParam(name = "commentId", value = "主键", required = false, paramType = "query" ,dataType="int"),
                @ApiImplicitParam(name = "commentName", value = "评论人", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentAccount", value = "评论帐号", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentContent", value = "评论内容", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentTime", value = "评论时间", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentCircleId", value = "分享圈id", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentLogo", value = "头像", required = false, paramType = "query" ,dataType="string"),
        })
    @RequestMapping("insertIgnoreNull")
    public Result insertIgnoreNull(@ApiIgnore CommentInfo commentInfo) {
    	try {
    		commentInfoMapper.insertIgnoreNull(commentInfo);
            return ResultGenerator.genSuccessResult();
       } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }
    
    
    /**
     * 修改，忽略null字段
     *
     * @param commentInfo 修改的记录
     * @return 返回影响行数
     */
    @ApiOperation(value = "修改，忽略null字段", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
                @ApiImplicitParam(name = "commentId", value = "主键", required = false, paramType = "query" ,dataType="int"),
                @ApiImplicitParam(name = "commentName", value = "评论人", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentAccount", value = "评论帐号", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentContent", value = "评论内容", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentTime", value = "评论时间", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentCircleId", value = "分享圈id", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "commentLogo", value = "头像", required = false, paramType = "query" ,dataType="string"),
        })
    @RequestMapping("updateIgnoreNull")
    public Result updateIgnoreNull(@ApiIgnore CommentInfo commentInfo) {
    	 try {
         	 commentInfoMapper.updateIgnoreNull(commentInfo);
            return ResultGenerator.genSuccessResult();
         } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }
    
    
     /**
     * 删除记录
     *
     * @param commentInfo 待删除的记录
     */
    @ApiOperation(value = "删除记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("delete")
    public Result delete(@ApiIgnore CommentInfo commentInfo) {
    	 try {
         	 commentInfoMapper.delete(commentInfo);
             return ResultGenerator.genSuccessResult();
         } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }


    @Autowired
    private CircleInfoMapper circleInfoMapper;

    /**
     * 根据account 查询 id
     */
    @RequestMapping("")
    public int findCirlelnfoId(String CirleAccount){
        CircleInfo cirleAccountByCirlelnfoId = circleInfoMapper.findCirleAccountByCirlelnfoId(CirleAccount);
        int num = commentInfoMapper.findCommentInfoIdByNum(cirleAccountByCirlelnfoId.getJzCircleId());
        return num;
    }


    
}